package com.hdax.shiro;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.hdax.filter.MyFormAuthenticationFilter;
import com.hdax.realm.UserRealm;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * shiro配置
 *
 * @author Aizen
 * @date 2022/05/04
 */
@Configuration
public class ShiroConfig {

    @Bean("hanyuPinyinOutputFormat")
    public HanyuPinyinOutputFormat getHanyuPinyinOutputFormat() {
        return new HanyuPinyinOutputFormat();
    }


    /**
     * shiro过滤器工厂bean
     * ShiroFilterFactoryBean:3第三步
     * @param defaultWebSecurityManager 默认web安全管理器
     * @return {@link ShiroFilterFactoryBean}
     */
    @Bean(name = "shiroFilterFactoryBean")
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        //设置登录页面的请求
        shiroFilterFactoryBean.setLoginUrl("/user/login");
        //登录成功后跳转的URL
        shiroFilterFactoryBean.setSuccessUrl("/index");
        //添加shiro的内置过滤器
        /**
         * anon: 无需认证就可以访问
         * authc: 必须认证了才能访问
         * user: 必须拥有 记住我功能 才能用
         * perms: 拥有对某个资源的权限才能访问
         * role: 拥有某个角色权限才能访问
         */
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/user/logout", "logout");
        filterMap.put("/layuimini/**", "anon");
        filterMap.put("/**", "authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        Map<String, Filter> map2 = new HashMap<String, Filter>();
        map2.put("authc", new MyFormAuthenticationFilter());
        shiroFilterFactoryBean.setFilters(map2);
        return shiroFilterFactoryBean;
    }

    /**
     * 默认web安全管理器
     * DefaultWebSecurityManager:2第二步
     * @param userRealm 用户领域
     * @return {@link DefaultWebSecurityManager}
     */
    @Bean(name="securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联UserRealm
        securityManager.setRealm(userRealm);
        ThreadContext.bind(securityManager);
        return securityManager;
    }

    /**
     * 获取用户领域
     * 创建Realm对象，需要自定义:1第一步
     * @return {@link UserRealm}
     */
    @Bean("userRealm")
    public UserRealm getUserRealm(HashedCredentialsMatcher hashedCredentialsMatcher) {
        UserRealm userRealm = new UserRealm();
        userRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return userRealm;
    }


    /**
     * 凭证匹配器
     * MD5加密
     * @return {@link HashedCredentialsMatcher}
     */
    @Bean
    public HashedCredentialsMatcher credentialsMatcher() {
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        credentialsMatcher.setHashAlgorithmName("md5");
        credentialsMatcher.setHashIterations(1);
        return credentialsMatcher;
    }

    /**
     * shiro过滤器
     *
     * @return {@link FilterRegistrationBean}
     */
    @Bean
    public FilterRegistrationBean getFilterRegistrationBean(){
        //创建一个过滤器注册处理对象
        FilterRegistrationBean filterBean = new FilterRegistrationBean();
        //创建一个代理过滤器 设置给该注册对象
        filterBean.setFilter(new DelegatingFilterProxy());
        //指定过滤器的名称  【必须和ShiroFilterFactoryBean组件的id 一致】
        filterBean.setName("shiroFilterFactoryBean");
        //设置参数
        filterBean.addInitParameter("targetFilterLifecycle","true");
        //设置拦截路径
        filterBean.addUrlPatterns("/*");
        return filterBean;
    }

    //thymeleaf 支持shiro标签
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }









}
